﻿#define _CRT_SECURE_NO_WARNINGS
#include"game.h"
void put(char board[ROWS][COLS], int rows, int cols, char set)
{
	for (int i = 0; i < rows; i++)
		for (int j = 0; j < cols; j++)
			board[i][j] = set;
}
void lei(char board[ROWS][COLS], int row, int col)
{
	int count = mine;
	while (count)
	{
		int i = rand() % row + 1;
		int j = rand() % col + 1;
		if (board[i][j] == '0')
		{
			board[i][j] = '1';
			count--;
		}
	}

}
void print(char board[ROWS][COLS], int row, int col)
{
	for (int i = 0; i <= row; i++)
		printf("%d ", i);

	printf("\n");
	for (int i = 1; i <= row; i++)
	{
		printf("%d ", i);
		for (int j = 1; j <= col; j++)
			printf("%c ", board[i][j]);
		printf("\n");

	}


}
int count_mine(char board[ROWS][COLS], int x, int y)
{
	int count = 0;
	for (int i = x - 1; i <= x + 1; i++)
	{
		for (int j = y - 1; j <= y + 1; j++)
		{
			if (board[i][j] == '1')
			{
				count++;
			}
		}
	}
	return count;
}
void finds(char m_board[ROWS][COLS], char s_board[ROWS][COLS], int row, int col, int x, int y, int* p)
{
	if (x >= 1 && x <= row && y >= 1 && y <= col)
	{
		int num = count_mine(m_board, x, y);
		if (num == 0)
		{
			(*p)++;
			s_board[x][y] = ' ';
			int i = 0;
			int j = 0;
			for (i = x - 1; i <= x + 1; i++)
			{
				for (j = y - 1; j <= y + 1; j++)
				{
					if (s_board[i][j] == '*')
					{

						finds(m_board, s_board, row, col, i, j, p);
					}
				}
			}
		}
		else
		{
			(*p)++;
			s_board[x][y] = (num + '0');
		}
	}
}
void find(char m_board[ROWS][COLS], char s_board[ROWS][COLS], int row, int col)
{
	int x = 0;
	int y = 0;
	int win = 0;
	int* p = &win;
	while (win < row * col - mine)
	{
		printf("请输入你要排查的坐标:>");
		scanf("%d %d", &x, &y);
		if (x >= 1 && x <= row && y >= 1 && y <= col)
		{
			if (m_board[x][y] == '1')
			{
				system("cls");
				printf("\n   很遗憾，你被炸死了！\n");
				print(m_board, row, col);
				break;
			}
			else
			{
				if (s_board[x][y] != '*')
				{
					printf("这个坐标查过了，请重新输入！\n");
					continue;
				}
				else
				{
					finds(m_board, s_board, row, col, x, y, p);
					system("cls");
					print(s_board, row, col);
				}


			}
		}
		else
		{
			printf("输入错误，请重新输入！\n");
		}
	}
	if (win == row * col - mine)
	{
		system("cls");
		printf("恭喜你，排雷成功！\n");
		print(s_board, row, col);
		return;

	}

}